<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline" :form="form">
        <a-row :gutter="48">
          <a-col :md="5" :sm="15">
            <a-form-item label="登录人">
              <a-input placeholder="请输入" v-model="queryParam.userName" />
            </a-form-item>
          </a-col>
          <a-col :md="5" d:sm="15">
            <a-form-item label="状态">
              <a-select placeholder="请选择" v-model="queryParam.success" >
                <a-select-option value="''">全部</a-select-option>
                <a-select-option value="'1'">成功</a-select-option>
                <a-select-option value="'0'">失败</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="9" :sm="18">
            <a-form-item label="登陆时间">
              <a-range-picker v-model="range" style="width: 100%" showTime showToday format="YYYY-MM-DD HH:mm:ss" />
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="12">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" icon="search" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button icon="sync" style="margin-left: 8px" @click="handleReset()">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <div class="table-operator">
      <a-popconfirm v-has="'monitor:logininfor:remove'" title="确认清空吗？" @confirm="clean">
        <a-icon slot="icon" type="question-circle-o" style="color: red" />
        <a-button type="danger" ghost icon="close">清空</a-button>
      </a-popconfirm>
      <a-dropdown v-has="'monitor:logininfor:remove'" v-if="selectedRowKeys.length > 0">
        <a-button type="danger" icon="delete" @click="delByIds(selectedRowKeys)">删除</a-button>
      </a-dropdown>
    </div>
    <s-table
      size="default"
      ref="table"
      rowKey="id"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      :columns="columns"
      :data="loadData"
      :rangPicker="range"
      defaultSort="createTime"
    >
      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 }}
      </span>
      <span slot="status" slot-scope="text">
        <a-badge :status="text | statusTypeFilter" :text="text | statusFilter" />
      </span>
    </s-table>
  </a-card>
</template>

<script>
import { STable } from '@/components'

import { getLoginLogList } from '@/api/monitor'
const statusMap = {
  1: {
    status: 'processing',
    text: '成功'
  },
  0: {
    status: 'error',
    text: '失败'
  }
}
export default {
  name: 'TableList',
  components: { STable },
  data() {
    return {
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      form: this.$form.createForm(this),
      mdl: {},
      permissions: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 表头
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '访问编号',
          dataIndex: 'id'
        },
        {
          title: '登录人',
          dataIndex: 'userName'
        },
        {
          title: '登录地址',
          dataIndex: 'ip'
        },
        {
          title: '浏览器',
          dataIndex: 'browserName'
        },
        {
          title: '操作系统',
          dataIndex: 'platformName'
        },
        {
          title: '系统名称',
          dataIndex: 'osName'
        },
        {
          title: '状态',
          dataIndex: 'success',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '操作时间',
          dataIndex: 'createTime'
        }
      ],
      range: null,
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        return getLoginLogList(Object.assign(parameter, this.queryParam))
      },
      commonStatus: [],
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  filters: {
    statusFilter (type) {
      return statusMap[type].text
    },
    statusTypeFilter (type) {
      return statusMap[type].status
    }
  },
  beforeCreate() {},
  methods: {
    handleReset() {
      this.queryParam = {
        userName: '',
        success: '',
        beginTime: '',
        endTime: ''
      }
      this.range=null
      this.handleOk()
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    handleOk() {
      this.$refs.table.refresh(true)
    },
    delByIds(ids) {
      this.$message.success(`你删除了` + ids)
      // delLoginLog({ ids: ids.join(',') }).then(res => {
      //   if (res.code === 0) {
      //     this.$message.success(`删除成功`)
      //     this.handleOk()
      //   } else {
      //     this.$message.error(res.msg)
      //   }
      //   this.selectedRowKeys = []
      // })
    },
    clean() {
      this.$message.success(`你点击了清空`)
      // cleanLoginLog().then(res => {
      //   this.handleOk()
      // })
    }
  },
  watch: {
    /*
      'selectedRows': function (selectedRows) {
        this.needTotalList = this.needTotalList.map(item => {
          return {
            ...item,
            total: selectedRows.reduce( (sum, val) => {
              return sum + val[item.dataIndex]
            }, 0)
          }
        })
      }
      */
  }
}
</script>
